<?php

class UsersDAO{
    
    public function UsersDAO(){
    
    }
    
	/**
     * @param ActorUser actorUser
     * @return true if the excecution was succesfull, false if opposite
     */
    public function addUSer($actorUser){
        $bd = new Bd();
        
        $sql = "INSERT INTO users (userName, name, lastName, email, password, access, userType) 
        		VALUES ('".$actorUser->getUserName()."', '".$actorUser->getName()."', '".$actorUser->getLastName()."', '".$actorUser->getEmail()."', '".$actorUser->getPassword()."', '".$actorUser->getAccess()."', '".$actorUser->getUserType()."');";
         
        return $bd->doUpdate($sql);
         
    }
    
    /**
     * @param ActorUser actorUser
     * @return true if the excecution was succesfull, false if opposite
     */
    public function modifyUSer($actorUser){
        $bd = new Bd();
        
        $sql = "UPDATE users SET userName = '".$actorUser->getUserName()."', name = '".$actorUser->getName()."', lastName = '".$actorUser->getLastName()."',
                email = '".$actorUser->getEmail()."', password = '".$actorUser->getPassword()."', access = '".$actorUser->getAccess()."',
                password = '".$actorUser->getPassword()."', userType = '".$actorUser->getUserType()."' WHERE id = ".$actorUser->getId().";";
        
        return $bd->doUpdate($sql);
         
    }

    /**
     * @param int $idUser > 0
     * @return array if the user exists, false otherwise
     */
    public function getUser($idUser){  
        if($this->existUser($idUser)){
            $bd = new Bd();        
            $sql = "SELECT * FROM users WHERE id = ".$idUser.";";
            return $bd->doQueryRow($sql);
        }
        return false;
    }
    
    /**
     * @param int $idUser > 0
     * @return true if the user exists, false otherwise
     */
    public function existUser($idUser){
        $bd = new Bd();
        
        $sql = "SELECT COUNT(*) FROM users WHERE id = ".$idUser.";";
        
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @param int $idUser > 0
     * @return true if the user could be deactivated, false otherwise or if it wasn't active
     */
    public function deactivateUser($idUser){
        if($this->isUserActive($idUser)){
            $bd = new Bd();        
            $sql = "UPDATE users SET access = 'f' WHERE id = ".$idUser.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }
    
    /**
     * @param int $idUser > 0
     * @return true if the user could be activated, false otherwise or if it wasn't inactive
     */
    public function activateUser($idUser){
        if(!$this->isUserActive($idUser)){
            $bd = new Bd();        
            $sql = "UPDATE users SET access = 't' WHERE id = ".$idUser.";";            
            return $bd->doUpdate($sql);
        }
        return false;
    }

    /**
     * @param int $idUser > 0
     * @return true if the user is active, false otherwise or if it doesn't exist
     */
    public function isUserActive($idUser){
        if($this->existUser($idUser)){
            $bd = new Bd();        
            $sql = "SELECT COUNT(*) FROM users WHERE id = ".$idUser." and access = 't';";            
            if ($bd->doQueryOne($sql) > 0)
                return true;
            else return false;
        }
        return false;
    }
    
    /**
     * @return array with all existing users
     */
    public function getUsers(){
        $bd = new Bd();        
        $sql = "SELECT * FROM users;";            
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @param string $email valid email
     * @return true if found false otherwise
     */
    public function searchUser($id){
        $bd = new Bd();        
        $sql = "SELECT * FROM users WHERE userId = ".$id." ;";
        return $bd->doQueryAll($sql);
    }
    
    /**
     * @param string $username valid userName
     * @return true if found false otherwise
     */
    public function existUserEmail($email){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM users WHERE email = '".$email."' ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @return array with all existing users
     */
    public function existUserName($username){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM users WHERE userName = '".$username."' ;";
        if ($bd->doQueryOne($sql) > 0)
            return true;
        else return false;
    }
    
    /**
     * @return int >= 0 with the matching number of users
     */
    public function howManyUsers(){
        $bd = new Bd();        
        $sql = "SELECT COUNT(*) FROM users;";
        return $bd->doQueryOne($sql);
    }
    
}

?>